layui.use(['treeTable'], function () {
        var treeTable = layui.treeTable;

        // 渲染树形表格
        var insTb = treeTable.render({
            elem: '#tree-table',
            url: ctx+'/sys/menu/list',
            method: "post",
            tree: {
                iconIndex: 0,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'id',  // id字段名称
                pidName: 'pid',    // pid字段名称
                openName: 'title',   // 自定义默认展开的字段名
            },
            cols: [[
                {field: 'title', minWidth:150, title: '菜单名称'},
                {field: 'url', minWidth:150, title: '菜单地址'},
                {field: 'perms', minWidth:150, title: '权限标识'},
                {field: 'menuType', minWidth:80, title: '菜单类型'
                	,templet: function(d){
    		    		return transDict("menuType",d.menuType);
    		    	}	
                },
                {field: 'sort', minWidth:80, title: '排序'},
                {field: 'remark', minWidth:150, title: '备注'}
                ,{
        			title: '操作',
        			align: 'center',
        			width: 150,
        			toolbar: '#menu-t-tool',
        			fixed: 'right'
        		}
                
            ]],
            parseData:  function(res) { 
    			return {
    				"code" : (res.code = 200 ? 0 : -1), 
    				"msg" : res.msg, 
    				"data" : res.data 
    			};
            }
       
        });

        // 搜索
        $('#menu-table-search').click(function () {
           /* var keywords = $('#searchWords').val();
            if (keywords) {
                insTb.filterData(keywords);
            } else {
                insTb.clearFilter();
            }
            */
        	var searchData = $("#menuSearchForm").serializeObject();
            insTb.reload({where:searchData});
        });

    // 全部展开
    $('#btnExpandAll').click(function () {
        insTb.expandAll();
    });

    // 全部折叠
    $('#btnFoldAll').click(function () {
        insTb.foldAll();
    });
	
	//新增页面
	$("#menuAdd").click(function(){
		var title = $(this).text();
		var url = ctx+"/sys/menu/addView";
		$.get(url, {}, function(res){
			if(!isJSON(res)){
			 var index = layer.open({
					type: 1,
					title: title,
					skin: 'layui-layer-rim', 
					area: ['35%', '75%'], 
					offset: ['0', '20%'],
			        content: res,
					btn: [],
					end: function(){
						pageReload();
					}
				    ,maxmin: true
			  });
				layer.full(index);
			}
		  });
	})

	//监听行工具
	treeTable.on('tool(tree-table)', function (obj) {
            var event = obj.event;
            if (event === 'del') {
              var id = obj.data.id
      	      layer.confirm('确定删除吗？', function(index){
      	    	  ajaxCommit({
      	    		  url : ctx+"/sys/menu/del?id="+id
      	    		  ,successfunc:function(){ pageReload() }
      	    	  })
      	      });
            } else if (event === 'edit') {
            	console.log(obj)
            	var title = $(this).text();
        		var id = obj.data.id
        		var url = ctx+"/sys/menu/editView?id="+id;
        		$.get(url, {}, function(res){
        			if(!isJSON(res)){
        			  var index = layer.open({
        					type: 1,
        					title: title,
        					skin: 'layui-layer-rim', 
        					area: ['35%', '75%'],
        					offset: ['0', '20%'],
        			        content: res,
        					btn: [],
        					end: function(){
        						pageReload();
        					}
        				    ,maxmin: true
        			   });
        				 layer.full(index);
        			}
        		  });
            }
    });
	

	
})
				
